/**
 * 
 */
package org.statewalker.graph.util;

import java.util.ArrayList;
import java.util.List;

import org.statewalker.graph.INodeWalkerListener;

/**
 * @author kotelnikov
 */
public class CompositeNodeWalkerListener<T, E extends Exception>
    implements
    INodeWalkerListener<T, E> {

    private List<INodeWalkerListener<T, E>> fList = new ArrayList<INodeWalkerListener<T, E>>();

    public void addListener(INodeWalkerListener<T, E> listener) {
        List<INodeWalkerListener<T, E>> list = new ArrayList<INodeWalkerListener<T, E>>(
            fList);
        list.add(listener);
        fList = list;
    }

    public boolean beginNode(T parent, T node) throws E {
        boolean result = true;
        List<INodeWalkerListener<T, E>> list = fList;
        for (INodeWalkerListener<T, E> listener : list) {
            result &= listener.beginNode(parent, node);
        }
        return result;
    }

    public void endNode(T parent, T node) throws E {
        List<INodeWalkerListener<T, E>> list = fList;
        for (INodeWalkerListener<T, E> listener : list) {
            listener.endNode(parent, node);
        }
    }

    public void removeListener(INodeWalkerListener<T, E> listener) {
        List<INodeWalkerListener<T, E>> list = new ArrayList<INodeWalkerListener<T, E>>(
            fList);
        list.remove(listener);
        fList = list;
    }

}
